﻿using System;
using System.Linq;
using Project.MVC.PhanQuyen.IDAL;
using Project.MVC.PhanQuyen.Model;

namespace Project.MVC.PhanQuyen.MsSQLDAL
{
    public class PQNhatKyRepository : IPQNhatKy
    {
        LinqPhanQuyenDataContext dataContext;

        public PQNhatKyRepository()
        {
            dataContext = new LinqPhanQuyenDataContext();
        }

        #region IPQNhatKy Members

        public IQueryable<PQNhatKyInfo> GetList()
        {
            try
            {
                var list = from record in dataContext.PQ_NhatKies
                           select new PQNhatKyInfo
                           {
                               Id = record.id,
                               TenNguoiDung = record.tenNguoiDung,
                               HoTenNguoiDung = record.hoTenNguoiDung,
                               SanGiaoDich = record.sanGiaoDich,
                               ThoiGian = record.thoiGian,
                               ThaoTac = record.thaoTac,
                               DiaChiIP = record.diaChi,
                               HeDieuHanh = record.heDieuHanh,
                               TrinhDuyet = record.trinhDuyet,
                               NoiDung = record.noiDung,
                           };
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Insert(PQNhatKyInfo value)
        {
            if (value == null)
                return;
            try
            {
                var info = new PQ_NhatKy
                {
                    tenNguoiDung = value.TenNguoiDung,
                    hoTenNguoiDung = value.HoTenNguoiDung,
                    sanGiaoDich = value.SanGiaoDich,
                    thoiGian = value.ThoiGian,
                    thaoTac = value.ThaoTac,
                    diaChi = value.DiaChiIP,
                    heDieuHanh = value.HeDieuHanh,
                    trinhDuyet = value.TrinhDuyet,
                    noiDung = value.NoiDung,
                };

                dataContext.PQ_NhatKies.InsertOnSubmit(info);

                dataContext.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Delete(PQNhatKyInfo value)
        {
            if (value == null)
                return;
            try
            {
                var info = (from record in dataContext.PQ_NhatKies
                            where record.id == value.Id
                            select record)
                           .SingleOrDefault();

                if (info != null)
                {
                    dataContext.PQ_NhatKies.DeleteOnSubmit(info);
                    dataContext.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


        public void Delete()
        {
            try
            {            
                dataContext.PQ_NhatKies.DeleteAllOnSubmit(dataContext.PQ_NhatKies);
                dataContext.SubmitChanges();             
                
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region IPQNhatKy Members


        public void Delete(int[] ids)
        {
            foreach (var id in ids)
            {
                var info = (from record in dataContext.PQ_NhatKies
                            where record.id == id
                            select record)
                           .SingleOrDefault();

                if (info != null)
                {
                    dataContext.PQ_NhatKies.DeleteOnSubmit(info);
                }
            }
            dataContext.SubmitChanges();
        }

        #endregion
    }
}